//package com.scs.application.modules.msg.provider;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.scs.application.core.exceptions.BusinessException;
//import com.scs.application.core.msg.domain.EmailMessage;
//import com.scs.application.core.msg.domain.RawMessage;
//import com.scs.application.core.utils.StringUtils;
//import com.scs.application.modules.msg.entity.PushedRecord;
//import com.scs.application.modules.msg.exception.SendMessageException;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.mail.javamail.JavaMailSender;
//import org.springframework.mail.javamail.JavaMailSenderImpl;
//import org.springframework.mail.javamail.MimeMessageHelper;
//import org.springframework.stereotype.Component;
//
//import javax.mail.MessagingException;
//import javax.mail.internet.MimeMessage;
//
//@Component
//@Slf4j
//public class SendEmailMessageProvider implements SendMessageProvider<EmailMessage> {
//
//    private JavaMailSender mailSender;
//
//    public SendEmailMessageProvider(JavaMailSender mailSender) {
//        this.mailSender = mailSender;
//    }
//
//
//    @Override
//    public boolean check(EmailMessage message) {
//        if (StringUtils.isBlank(message.getTo())) {
//            throw new BusinessException("邮件消息校验失败，收件人不能为空");
//        }
//        if (StringUtils.isBlank(message.getTitle())) {
//            throw new BusinessException("邮件消息校验失败，邮件标题不能为空");
//        }
//        return true;
//    }
//
//    @Override
//    public boolean support(Class<? extends RawMessage> clz) {
//        return (EmailMessage.class.isAssignableFrom(clz));
//    }
//
//    /**
//     * 构建pushRecord
//     *
//     * @param pushedRecord
//     * @param emailMessage
//     * @return
//     */
//    @Override
//    public PushedRecord getPushedRecord(PushedRecord pushedRecord, EmailMessage emailMessage) {
//        pushedRecord.setTitle(emailMessage.getTitle())
//                .setContent(emailMessage.getContent())
//                .setReceiver(emailMessage.getTo())
//                .setParams(emailMessage.getParams() != null ? JSONObject.parseObject(JSON.toJSONString(emailMessage.getParams())) : null);
//        return pushedRecord;
//    }
//
//
//    @Override
//    public void send(EmailMessage emailMessage) throws SendMessageException {
//
//        MimeMessage mimeMessage = mailSender.createMimeMessage();
//        try {
//            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
//            message.setFrom(((JavaMailSenderImpl) mailSender).getUsername());
//            message.setTo(emailMessage.getTo());
//            message.setSubject(emailMessage.getTitle());
//            message.setText(emailMessage.getContent(), true);
//            mailSender.send(mimeMessage);
//
//        } catch (MessagingException e) {
//            throw new SendMessageException("邮件发送失败", e);
//        }
//        log.info("邮件发送成功，接收邮箱：{}", emailMessage.getTo());
//    }
//}
